/**
 * 作者：陈文斌 on 2018/12/29.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'package:flutter/material.dart';
import 'package:crm/constant/constant.dart';
import 'package:crm/viewModel/Login/login_view_model.dart';

class TextPasswordSection extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _TextPasswordSectionState();
}

class _TextPasswordSectionState extends State<TextPasswordSection> {
  String _pass = '';

  @override
  Widget build(BuildContext context) {
    // 输入框管理器
    TextEditingController _passController = TextEditingController.fromValue(
      TextEditingValue(
        text: _pass,
        selection: TextSelection.fromPosition(
          TextPosition(affinity: TextAffinity.downstream, offset: _pass.length),
        ),
      ),
    );

    Widget _passwordTextFileSection() {
      return Container(
        width: AppScreen.setWidth(300),
        height: 37,
        margin: EdgeInsets.fromLTRB(
          AppScreen.setWidth(38),
          40,
          AppScreen.setWidth(37),
          0,
        ),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: AppColors.LineColor,
              width: 1,
            ),
          ),
        ),
        child: TextField(
          controller: _passController,
          textAlign: TextAlign.center,
          obscureText: true,
          style: TextStyle(
            color: AppColors.TextColor,
            fontSize: AppFontSizes.Font15,
            fontFamily: AppFontFamilies.PingFangSC,
            fontWeight: FontWeight.w500,
          ),
          decoration: new InputDecoration(
            contentPadding: EdgeInsets.only(top: 5),
            hintText: '请输入登录密码',
            border: InputBorder.none,
            hintStyle: TextStyle(
              color: AppColors.PlaceholderTextColor,
              fontSize: AppFontSizes.Font15,
              fontFamily: AppFontFamilies.PingFangSC,
              fontWeight: FontWeight.w500,
            ),
          ),
          textInputAction: TextInputAction.done,
          onChanged: (str) {
            _pass = str;
            setState(() {});
          },
          onEditingComplete: () {
            FocusScope.of(context).requestFocus(FocusNode());
          },
        ),
      );
    }

    Widget _loginButtonSection() {
      Color _defaultColor = Color(0xFF9AD1FF);
      Color _highlightColor = Color(0xFF3C86F3);
      bool _isHighlight = _pass.length > 0;

      return Container(
        width: AppScreen.setWidth(300),
        height: 42,
        margin: EdgeInsets.only(
          left: AppScreen.setWidth(36),
          top: AppScreen.setHeight(63),
          right: AppScreen.setWidth(36),
        ),
        decoration: BoxDecoration(
          color: !_isHighlight ? _defaultColor : _highlightColor,
          borderRadius: BorderRadius.circular(21),
        ),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(21),
          child: MaterialButton(
            onPressed: () {
              FocusScope.of(context).requestFocus(FocusNode());
              LoginVM.crmLogin(context: context,password: _pass,loginType: 1,);
            },
            child: Text(
              '登录',
              style: TextStyle(
                fontSize: AppFontSizes.Font15,
                fontFamily: AppFontFamilies.PingFangSC,
                fontWeight: FontWeight.w500,
              ),
            ),
            textColor: Colors.white,
          ),
        ),
      );
    }

    return Column(
      children: <Widget>[
        _passwordTextFileSection(),
        _loginButtonSection(),
      ],
    );
  }
}
